//
//  MyGradeTableHeaderView.m
//  MyGrade
//
//  Created by 田涛 on 2017/9/28.
//  Copyright © 2017年 Lemon. All rights reserved.
//

#import "MyGradeTableHeaderView.h"
#import <SDAutoLayout.h>
#import <SDWebImage/UIButton+WebCache.h>
#import "ZYProGressView.h"
#import "GlobalHeader.h"


@interface MyGradeTableHeaderView ()
/** 头像*/
@property (nonatomic, strong)  UIButton *headerImgBtn;
/** 头像下面的装饰*/
@property (nonatomic, strong)  UIButton *headerImgBottomBtn;
/** 我的经验值*/
@property (nonatomic, strong) UILabel *myExperienceLbl;
/** 击败平台用户*/
@property (nonatomic, strong)  UILabel *defeatLbl;
/** 进度条左边等级*/
@property (nonatomic, strong)  UILabel *leftGrade;
/** 进度条右边等级*/
@property (nonatomic, strong)  UILabel *rightGrade;
/** 进度条*/
@property (nonatomic, strong) ZYProGressView *progressView;
/** 进度条 进度*/
@property (nonatomic, strong)  UILabel *progressLbl;
@end

@implementation MyGradeTableHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor blueColor];
        [self setup];
        [self layout];
    }
    return self;
}


- (void)setup {
    
    [self addSubview:self.headerImgBtn];
    [self addSubview:self.headerImgBottomBtn];
    [self addSubview:self.myExperienceLbl];
    [self addSubview:self.defeatLbl];
    [self addSubview:self.progressView];
    [self addSubview:self.leftGrade];
    [self addSubview:self.rightGrade];
    [self addSubview:self.progressLbl];
}

- (void)setModel:(MyGradeHeaderViewModel *)model {
    _model = model;
    [_headerImgBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:model.headerImgUrl]
                                       forState:UIControlStateNormal
                               placeholderImage:[UIImage imageNamed:@""]];
    [_headerImgBottomBtn setTitle:[NSString stringWithFormat:@"LV.%ld",model.grade] forState:UIControlStateNormal];
    _myExperienceLbl.text = [NSString stringWithFormat:@"我的经验值：%ld",model.myCurrentExperience];
    
    NSMutableString *defeatNum = [NSMutableString stringWithFormat:@"%.0f",model.defeatNum];
    [defeatNum appendString:@"%"];
    _defeatLbl.text = [NSString stringWithFormat:@"击败平台%@用户",defeatNum];
    
    _leftGrade.text = [NSString stringWithFormat:@"LV.%ld",model.grade];
    _rightGrade.text = [NSString stringWithFormat:@"LV.%ld",model.nextGrade];   // 进度条右边的等级
    
    CGFloat mycurrent     = [[NSString stringWithFormat:@"%ld",model.myCurrentExperience] floatValue];
    CGFloat allExperience = [[NSString stringWithFormat:@"%ld",model.allExperience] floatValue];
    _progressView.progressValue = [NSString stringWithFormat:@"%lf",mycurrent/allExperience];
    
    _progressLbl.text = [NSString stringWithFormat:@"%ld/%ld",model.myCurrentExperience,model.allExperience];
}

- (void)layout {
    self.myExperienceLbl.sd_layout
    .leftSpaceToView(self, SCREEN_WIDTH *0.126)
    .topSpaceToView(self.headerImgBottomBtn, SCREEN_HEIGHT *0.02)
    .heightIs(HeightScale_IOS7(20));
    [self.myExperienceLbl setSingleLineAutoResizeWithMaxWidth:SCREEN_WIDTH/2];
    
    self.defeatLbl.sd_layout
    .rightSpaceToView(self, SCREEN_WIDTH *0.11)
    .topEqualToView(self.myExperienceLbl)
    .heightIs(HeightScale_IOS7(20));
    [self.defeatLbl setSingleLineAutoResizeWithMaxWidth:SCREEN_WIDTH/2];
    
    self.progressView.sd_layout
    .centerXEqualToView(self)
    .topSpaceToView(self.myExperienceLbl, HeightScale_IOS7(18))
    .widthIs(SCREEN_WIDTH *0.64)
    .heightIs(4);
    
    self.leftGrade.sd_layout
    .rightSpaceToView(self.progressView, WidthScale_IOS7(8))
    .centerYEqualToView(self.progressView)
    .heightIs(HeightScale_IOS7(15));
    [self.leftGrade setSingleLineAutoResizeWithMaxWidth:80];
    
    self.rightGrade.sd_layout
    .leftSpaceToView(self.progressView, WidthScale_IOS7(8))
    .centerYEqualToView(self.progressView)
    .heightIs(HeightScale_IOS7(15));
    [self.leftGrade setSingleLineAutoResizeWithMaxWidth:80];
    
    self.progressLbl.sd_layout
    .rightEqualToView(self.progressView)
    .heightIs(HeightScale_IOS7(10))
    .topSpaceToView(self.progressView, HeightScale_IOS7(8));
    [self.progressLbl setSingleLineAutoResizeWithMaxWidth:80];
}


#pragma mark - 懒加载
- (UIButton *)headerImgBtn {
    if (!_headerImgBtn) {
        CGFloat btnWidth = self.frame.size.width *0.197;
        _headerImgBtn = [[UIButton alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - btnWidth )/2, HeightScale_IOS7(60), btnWidth, btnWidth)];
        _headerImgBtn.layer.cornerRadius  = btnWidth /2;
        _headerImgBtn.layer.masksToBounds = YES;
        _headerImgBtn.layer.borderWidth   = 5;
        _headerImgBtn.layer.borderColor   = [UIColor colorWithRed:(117/255.0) green:(69/255.0) blue:(254/255.0) alpha:1].CGColor;
    }
    return _headerImgBtn;
}

- (UIButton *)headerImgBottomBtn {
    if (!_headerImgBottomBtn) {
        CGFloat btnWidth  = self.frame.size.width *0.25;
        CGFloat btnHeight = self.frame.size.height *0.09;
        _headerImgBottomBtn = [[UIButton alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - btnWidth )/2, CGRectGetMaxY(_headerImgBtn.frame) -HeightScale_IOS7(12), btnWidth, btnHeight)];
        [_headerImgBottomBtn setBackgroundImage:[UIImage imageNamed:@"gradeStyle"] forState:UIControlStateNormal];
        _headerImgBottomBtn.titleLabel.font = [UIFont systemFontOfSize:HeightScale_IOS7(14)];
    }
    return _headerImgBottomBtn;
}

- (UILabel *)myExperienceLbl {
    if (!_myExperienceLbl) {
        _myExperienceLbl = [[UILabel alloc] init];
        _myExperienceLbl.textColor = [UIColor whiteColor];
        _myExperienceLbl.font = [UIFont systemFontOfSize:HeightScale_IOS7(14)];
    }
    return _myExperienceLbl;
}

- (UILabel *)defeatLbl {
    if (!_defeatLbl) {
        _defeatLbl = [[UILabel alloc] init];
        _defeatLbl.textColor = [UIColor whiteColor];
        _defeatLbl.font = [UIFont systemFontOfSize:HeightScale_IOS7(14)];
    }
    return _defeatLbl;
}

- (ZYProGressView *)progressView {
    if (!_progressView) {
        CGFloat width = SCREEN_WIDTH *0.64;
        _progressView = [[ZYProGressView alloc] initWithFrame:CGRectMake(0 , 0, width, HeightScale_IOS7(4))];
        _progressView.progressValue = @"0.7";
        _progressView.progressColor = [UIColor colorWithRed:(184/255.0) green:(0/255.0) blue:(249/255.0) alpha:1];
    }
    return _progressView;
}

- (UILabel *)leftGrade {
    if (!_leftGrade) {
        _leftGrade = [[UILabel alloc] init];
        _leftGrade.textColor = [UIColor whiteColor];
        _leftGrade.font = [UIFont systemFontOfSize:HeightScale_IOS7(15)];
    }
    return _leftGrade;
}

- (UILabel *)rightGrade {
    if (!_rightGrade) {
        _rightGrade = [[UILabel alloc] init];
        _rightGrade.textColor = [UIColor whiteColor];
        _rightGrade.font = [UIFont systemFontOfSize:HeightScale_IOS7(15)];
    }
    return _rightGrade;
}

- (UILabel *)progressLbl {
    if (!_progressLbl) {
        _progressLbl = [[UILabel alloc] init];
        _progressLbl.font = [UIFont systemFontOfSize:HeightScale_IOS7(12)];
        _progressLbl.textColor = [UIColor whiteColor];
    }
    return _progressLbl;
}

@end

